<template>
  <div>
    <!-- 头部标签 -->
    <van-nav-bar placeholder fixed>
      <template #left>
        <img src="../../assets/toutiao_logo.png" alt="" class="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="20"  @click="toSearchFn"/>
      </template>
    </van-nav-bar>
    <!-- 标题导航  -->
    <van-tabs v-model="active" color="yellow" sticky offset-top="1.22666667rem">
      <van-tab
        :title="obj.name"
        v-for="obj in channelsList"
        :key="obj.id"
        :name="obj.id"
      >
        <ArticleList :channel_id="obj.id"></ArticleList>
      </van-tab>
    </van-tabs>
    <!-- ＋ 号图标 -->
    <van-icon
      name="plus"
      size="0.4rem"
      class="moreChannels"
      @click="popupShowFn"
    />
    <!-- 加号 弹出层 -->
    <van-popup v-model="show" class="popupstyle">
      <ChannelEdit
      @closePopup="closePopupBtnFn"
      :channelsList="channelsList"
      @addChannel="addFn"
      v-model="active"
      ></ChannelEdit>
    </van-popup>
    <!-- 底部反馈面板 -->
    <van-action-sheet
      v-model="$store.state.homeArticelActions"
      :actions="actionsArr"
      :cancel-text="canceltext"
      @select="onSelect"
      @click="cancelFn"
      @close="closeActionFn"
      get-container="body"
    />
  </div>
</template>

<script>
import { getChannelsApi, disLikeApi, reportsArticleApi, getUserAddChannelApi } from '@/api'
import ArticleList from './ArticleList'
import ChannelEdit from './components/ChannelEdit'
import { mapState, mapMutations } from 'vuex'
import { Toast } from 'vant'

export default {
  data () {
    return {
      channelsList: [], // 用户选择频道列表
      active: 0, // 频道id
      actionsArr: [], // 反馈面板变量数据
      show: false, // 弹出层
      // actions: [{ name: '不感兴趣' }, { name: '反馈垃圾内容' }]
      canceltext: '取消', // 反馈面板取消
      newArr: [] // 要传给后台的数组
    }
  },
  computed: {
    ...mapState(['firstActions', 'secondActions', 'articeArtID'])
  },
  methods: {
    ...mapMutations(['changeHomeArticelActions']),
    // 底部反馈
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (item.name === '反馈垃圾内容') {
        this.actionsArr = this.secondActions
        this.canceltext = '返回'
      } else if (item.name === '不感兴趣') {
        // 对文章的不喜欢
        const res = await disLikeApi(this.articeArtID)
        console.log(res)
        this.changeHomeArticelActions(false)
        Toast.success('反馈成功')
      } else {
        // 二级面板
        const res = await reportsArticleApi({
          target: this.articeArtID,
          type: item.value
        })
        console.log(res)
        // 内容换成一级
        // 关闭
        this.changeHomeArticelActions(false)
        setTimeout(() => {
          this.actionsArr = this.firstActions
        }, 200)
        // 换文字
        this.canceltext = '取消'
        // 给个提示
        Toast.success('反馈成功')
      }
    },
    // 返回/取消按钮切换
    cancelFn () {
      if (this.canceltext === '返回') {
        this.changeHomeArticelActions(true)
        this.actionsArr = this.firstActions
        this.canceltext = '取消'
      }
    },
    // 弹出层按钮
    popupShowFn () {
      this.show = true
    },
    // 子传父 关闭弹出层
    closePopupBtnFn (bool) {
      this.show = bool
    },
    // 切换关闭弹出层
    closeActionFn () {
      // 如果打开二级反馈面板 点击屏幕取消 切换标题再次打开反馈面板 默认显示一级面板
      this.actionsArr = this.firstActions
    },
    // 用户选择 - 添加频道
    async  addFn (pushChannelObj) {
      // 清空数组
      this.newArr = []
      // 添加到页面
      this.channelsList.push(pushChannelObj)

      this.channelsList.forEach((obj, index) => {
        if (obj !== 0) {
          // 创建新对象 展开赋值
          const newObj = { ...obj }
          // console.log(newObj)
          // 删除name属性名
          delete newObj.name
          // 放进添加新的属性名seq
          newObj.seq = index
          // 把新对象添加到新数组里
          this.newArr.push(newObj)
          // console.log(this.newArr)
        }
      })
      // 用户添加频道更新到后台
      await getUserAddChannelApi(this.newArr)
      // console.log(res)
    },
    toSearchFn () {
      this.$router.push('/serch')
    }
  },
  // 注册组件
  components: {
    // 注册组件
    ArticleList,
    ChannelEdit
  },
  // 获取用户推荐频道列表 页面刚打开就加载
  async created () {
    const res = await getChannelsApi()
    // console.log(res)
    this.channelsList = res.data.data.channels
    this.actionsArr = this.firstActions
  },
  // 销毁全局组件  关闭弹窗
  destroyed () {
    this.changeHomeArticelActions(false)
  }
}
</script>

<style lang="less" scoped>
/deep/ .logo {
  width: 100px;
  height: 30px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 59px;
  right: 8px;
  z-index: 999;
}

.popupstyle {
  background-color: transparent;
  height: 100%;
  width: 100%;
  background-color: #fff;
}
</style>>
